package com.changgou.service.impl;

import com.changgou.service.WeiXinPayService;
import com.github.wxpay.sdk.WXPayUtil;
import entity.HttpClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 秦川熙
 */
@Service
public class WeiXinPayServiceImpl implements WeiXinPayService {

    /**
     * 微信公众账号或开放平台APP的唯一标识
     */
    @Value("${weixin.appid}")
    private String appid;

    /**
     * 财付通平台的商户账号
     */
    @Value("${weixin.partner}")
    private String partner;

    /**
     * 财付通平台的商户密钥
     */
    @Value("${weixin.partnerkey}")
    private String partnerkey;

    /**
     * 回调地址
     */
    @Value("${weixin.notifyurl}")
    private String notifyurl;

    /**
     * 创建二维码
     * @param parameter :其他附加信息
     * @return
     */
    @Override
    public Map createNative(Map<String, String> parameter) {
        try {
            //1、封装参数
            Map param = new HashMap();
            //应用ID
            param.put("appid", appid);
            //商户ID号
            param.put("mch_id", partner);
            //随机数
            param.put("nonce_str", WXPayUtil.generateNonceStr());
            //订单描述
            param.put("body", "畅购");
            //商户订单号
            param.put("out_trade_no", parameter.get("outtradeno"));
            //交易金额
            param.put("total_fee", parameter.get("money"));
            //终端IP
            param.put("spbill_create_ip", "127.0.0.1");
            //回调地址
            param.put("notify_url", notifyurl);
            //交易类型
            param.put("trade_type", "NATIVE");

            //2、将参数转成xml字符， 并携带签名
            String paramXml = WXPayUtil.generateSignedXml(param, partnerkey);

            //3、执行请求  https://api.mch.weixin.qq.com/pay/unifiedorder:调用远程支付接口
            HttpClient httpClient = new HttpClient("https://api.mch.weixin.qq.com/pay/unifiedorder");
            httpClient.setHttps(true);
            httpClient.setXmlParam(paramXml);
            httpClient.post();

            //4、获取参数
            String content = httpClient.getContent();
            Map<String, String> resultMap = WXPayUtil.xmlToMap(content);
            System.out.println("resultMap:" + resultMap);

            //5、获取部分页面所需参数
            Map<String, String> dataMap = new HashMap<String, String>();
            //二维码链接
            dataMap.put("code_url", resultMap.get("code_url"));
            dataMap.put("out_trade_no", parameter.get("outtradeno"));
            dataMap.put("total_fee", parameter.get("money"));

            return dataMap;
        }  catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 查询订单状态
     * @param out_trade_no
     * @return
     */
    @Override
    public Map queryPayStatus(String out_trade_no) {

        try {
            //1、封装参数
            Map param = new HashMap();
            //应用ID
            param.put("appid", appid);
            //商户ID号
            param.put("mch_id", partner);
            //随机数
            param.put("nonce_str", WXPayUtil.generateNonceStr());
            //商户订单号
            param.put("out_trade_no", out_trade_no);

            //2、将参数转成xml字符， 并携带签名
            String paramXml = WXPayUtil.generateSignedXml(param, partnerkey);

            //3、发送请求
            HttpClient httpClient = new HttpClient("https://api.mch.weixin.qq.com/pay/orderquery");
            httpClient.setHttps(true);
            httpClient.setXmlParam(paramXml);
            httpClient.post();

            //4、获取返回值， 并将返回值转换成Map
            String content = httpClient.getContent();
            return WXPayUtil.xmlToMap(content);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 关闭微信支付
     * @param orderId
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, String> closePay(Long orderId) throws Exception {
        //参数设置
        Map<String, String> paramMap = new HashMap<String, String>();
        //应用id
        paramMap.put("appid", appid);
        //商户编号
        paramMap.put("mch_id", partner);
        //随机字符
        paramMap.put("nonce_str", WXPayUtil.generateNonceStr());
        //商家的唯一编号
        paramMap.put("out_trade_no", String.valueOf(orderId));

        //将Map数据转成xml字符
        String xmlParam = WXPayUtil.generateSignedXml(paramMap, partnerkey);

        //确定url
        String url = "https://api.mch.weixin.qq.com/pay/closeorder";

        //发送请求
        HttpClient httpClient = new HttpClient(url);
        //https
        httpClient.setHttps(true);
        //提交参数
        httpClient.setXmlParam(xmlParam);

        //提交
        httpClient.post();

        //获取返回数据
        String content = httpClient.getContent();
        //将返回数据解析成Map
        return WXPayUtil.xmlToMap(content);
    }
}
